package com.bookmall.product.web;

import com.alibaba.fastjson.JSON;
import com.bookmall.product.domain.BookCategory;
import com.bookmall.product.domain.BookInfoMessage;
import com.bookmall.product.domain.vo.BookInfoVo;
import com.bookmall.product.domain.vo.BookMessageVo;
import com.bookmall.product.service.IBookInfoMessageService;
import com.bookmall.product.service.IBookInfoService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Description:
 * @Author 胡菜鸡
 * @Create 2022-03-17-22:26
 */
@Controller
public class ItemController {

    @Autowired
    private IBookInfoService bookInfoService;

    @Autowired
    private IBookInfoMessageService bookInfoMessageService;

    @Autowired
    private ThreadPoolExecutor executor;

    @GetMapping("/item/{bookId}.html")
    @ResponseBody
    public String bookItem(@PathVariable("bookId") String bookId) {
        System.out.println("准备查询" + bookId + "书籍的详情");
        BookInfoVo bookInfoVo = bookInfoService.selectBookInfoById(bookId);
        bookInfoVo.setImagesArray(bookInfoVo.getImageUrl().split(","));


//        model.addAttribute("bookItem", bookInfoVo);
        return JSON.toJSONString(bookInfoVo);
    }

    @GetMapping("/item.html")
    public String item(@RequestParam("bookId") String bookId, Model model) throws ExecutionException, InterruptedException {
        CompletableFuture<Void> bookBaseInfo = CompletableFuture.runAsync(() -> {
            //查询书籍基本信息
            BookInfoVo bookInfoVo = bookInfoService.selectBookInfoById(bookId);
            bookInfoVo.setBookImageList(Arrays.asList(bookInfoVo.getImageUrl().split(",")));
            model.addAttribute("bookItem", bookInfoVo);
        }, executor);

        CompletableFuture<Void> bookMessageInfo = CompletableFuture.runAsync(() -> {
            //查询书籍评论信息
            BookMessageVo bookMessageVo = bookInfoMessageService.selectBookMessageByBookId(bookId);
            model.addAttribute("bookMessage", bookMessageVo);
        }, executor);

        CompletableFuture<Void> bookRecommendInfo = CompletableFuture.runAsync(() -> {
            //查看推荐书籍
            List<BookInfoVo> recommendBooks = bookInfoService.selectLatestBookInfoList();
            model.addAttribute("recommendBooks", recommendBooks);
        }, executor);

        CompletableFuture.allOf(bookBaseInfo, bookMessageInfo, bookRecommendInfo).get();
        return "item";
    }


    /**
     * 评论方法
     * @author 胡菜鸡
     * @date 2022/5/3 17:07
     */
    @PostMapping("/comment.html")
    public String comment(BookInfoMessage bookInfoMessage){
        bookInfoMessageService.addComment(bookInfoMessage);
        return "redirect:http://huyulu666.top/item.html?bookId=" + bookInfoMessage.getBookId();
    }



    /**
     * 添加商品到喜欢
     * @author 胡菜鸡
     * @date 2022/5/3 19:01
     */
    @PostMapping("/addToLikes")
    @ResponseBody
    public String addToLikes(String bookId)
    {
        Boolean result = bookInfoService.addToLikes(bookId);
        return JSON.toJSONString(result);
    }
}
